rm(list = ls())
library(tidyverse)
library(magrittr)
library(cowplot)
library(ggplot2)
devtools::load_all()
# USAEPU to CHNTFP
# USAEPU to USATFP
if (FALSE){
  p <- list()
  EPUtype <- c('eco','fiscal','money','trade')
  # EPUtype <- 'eco'
  pictxt <- 'p[[1]]'
  for (i in 1:length(EPUtype)) {
    p[[i]] <- ConsPic(impvar =  'unctn',impcnt =  'USA', responseCnt = 'CHN',resvar = 'TFP',patnm = '',
                      responseCnt_pic = 'USA', resvar_pic = 'TFP',EPUtype = EPUtype[i], indus = '', p = 2) + labs(title = EPUtype[i])
    if (i != 1) pictxt <- paste(pictxt,',p[[',as.character(i),']]',sep = '')
  }
  pictxt <- paste('plot_grid(',pictxt,',ncol = 1)',sep = '')
  eval(parse(text = pictxt))
  ggsave(filename = './data-raw/USAunctn2USATFP.wmf')
}

# 传导机制USAEPU-USATFP
if (FALSE){
  p <- list()
  EPUtype <- c('eco','fiscal','money','trade')
  pictxt <- 'p[[1]]'
  for (i in 1:length(EPUtype)) {
    p[[i]] <- ConsPic(impvar =  'unctn',impcnt =  'USA', responseCnt = 'CHN',resvar = 'cpi',patnm = '_pat',
                      EPUtype = EPUtype[i], indus = '', p = 1) + labs(title = EPUtype[i])
    if (i != 1) pictxt <- paste(pictxt,',p[[',as.character(i),']]',sep = '')
  }
  pictxt <- paste('plot_grid(',pictxt,',ncol = 2)',sep = '')
  eval(parse(text = pictxt))
  ggsave(filename = './data-raw/USAEPU2CHNEPU.wmf')
}

# 行业分析
if (FALSE){
  # indus <- c("电气机械及器材制造业","纺织服装.服饰业","纺织业", "化学纤维制造业",
  #            "计算机.通信和其他电子设备制造业", "家具制造业","酒.饮料和精制茶制造业",
  #            "木材加工及木.竹.藤.棕.草制品业","农副食品加工业", "食品制造业",
  #            "通用设备制造业","医药制造业", "有色金属冶炼及压延加工业","专用设备制造业","造纸及纸制品业")
  indus <- c("纺织服装.服饰业","纺织业", "家具制造业","酒.饮料和精制茶制造业",
             "食品制造业","有色金属冶炼及压延加工业","电气机械及器材制造业",
             "计算机.通信和其他电子设备制造业",
             "通用设备制造业","专用设备制造业","医药制造业")
  p <- list()
  # EPUtype <- c('eco','fiscal','money','trade','yest','tomo')
  pictxt <- 'p[[1]]'
  for (i in 1:length(indus)) {
    p[[i]] <- ConsPic(impvar =  'cpi',impcnt =  'CHN', responseCnt = 'CHN',resvar = 'TFP',patnm = '',
                      EPUtype = 'eco', indus = indus[i], p = 2) + labs(title = indus[i])
    if (i != 1) pictxt <- paste(pictxt,',p[[',as.character(i),']]',sep = '')
  }
  pictxt <- paste('plot_grid(',pictxt,',ncol = 2)',sep = '')
  eval(parse(text = pictxt))
  ggsave(filename = './data-raw/CHNEPU2CHNindus2e.wmf', width = 15, height = 20, units = 'cm')
}
